//
//  ExAutoHeightTableViewCell.m
//  OC-Example
//
//  Created by apang on 2018/7/31.
//  Copyright © 2018年 apang.men All rights reserved.
//

#define FontSize    14.0

#define Margin  10.0

#import "ExAutoHeightTableViewCell.h"

@interface ExAutoHeightTableViewCell ()

@property (nonatomic , strong) UILabel * textContentLabel;

@end

@implementation ExAutoHeightTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        //        [self.contentView setBackgroundColor:[UIColor grayColor]];
        
        _textContentLabel = [[UILabel alloc] init];
        
        [_textContentLabel setFont:[UIFont systemFontOfSize:FontSize]];
        
        [_textContentLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
        
        [_textContentLabel setNumberOfLines:0];
        
        //        [_textContentLabel setBackgroundColor:[UIColor greenColor]];
        
        [self.contentView addSubview:_textContentLabel];
        
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(m)-[_textContentLabel]-(m)-|" options:0 metrics:@{@"m":@(Margin)} views:NSDictionaryOfVariableBindings(_textContentLabel)]];
        
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(m)-[_textContentLabel]-(m)-|" options:0 metrics:@{@"m":@(Margin)} views:NSDictionaryOfVariableBindings(_textContentLabel)]];
    }
    
    return self;
}

- (void)setTextContent:(NSString *)textContent {
    
    _textContent = textContent;
    
    _textContentLabel.text = textContent;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

+(CGFloat)cellHeightWithString:(NSString *)string {
    
    CGFloat maxWidth = SCREEN_BOUNDS.width - Margin - Margin;
    
    CGSize maxSize = CGSizeMake(maxWidth, CGFLOAT_MAX);
    
    UIFont *font = [UIFont systemFontOfSize:FontSize];
    
    CGRect rect = [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
    
    //因为该计算方法和Label的实际占位略有出入所以要加上容错值
    return Margin + Margin + rect.size.height +10.0;
}
@end
